package com.tubruk.todo.database;

import java.util.ArrayList;
import java.util.Date;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.tubruk.todo.TaskContent;
import com.tubruk.todo.Todos;
import com.tubruk.util.Util;

public class TodoAdapter {

	public static final int insertNewRow(ContentResolver resolver, TaskContent task){
		ContentValues content = new ContentValues();
		
		content.put(Todos.Todo.TITLE, task.getTitle());
		content.put(Todos.Todo.NOTE, task.getNote());
		content.put(Todos.Todo.PRIORITY, task.getPriority());
		
		
		if (task.getParentId() != -1){
			content.put(Todos.Todo.PARENT_ID, task.getParentId());
		}
		
		if (task.getDueDate() != null){
			content.put(Todos.Todo.DUE_DATE, task.getDueDate().getTime());
		}
		
		task.setCreatedDate(new Date());
		task.setModifiedDate(task.getCreatedDate());
		
		task.setDone(TaskContent.DONE_NO);
		
		String time = new StringBuffer("").append(task.getCreatedDate().getTime()).toString();
	
		content.put(Todos.Todo.CREATED_DATE, time);
		content.put(Todos.Todo.MODIFIED_DATE, time);
		
		Uri uri = resolver.insert(Todos.Todo.CONTENT_URI, content);		
		
		//ripta: will need to check whether it was a success....
		if (uri != null) {
			return 1;
		}
		return 0;
	}
	
	
	public static final int updateComplete(ContentResolver resolver, int complete, int id){
		ContentValues content = new ContentValues();
		content.put(Todos.Todo.DONE, complete);
		
		Date now = new Date();
		String time = new StringBuffer("").append(now.getTime()).toString();
		
		content.put(Todos.Todo.MODIFIED_DATE, time);

		return resolver.update(Todos.Todo.CONTENT_URI, content, "_id=?", new String[]{Integer.toString(id)});
	}

	public static final int delete(ContentResolver resolver, int id){
		Log.d("TODOADAPTER", "DELETING ID: " + id);
		
		String[] param = new String[]{Integer.toString(id)};
		
		int result = resolver.delete(Todos.Todo.CONTENT_URI, "parent_id=?", param);
		result = result + resolver.delete(Todos.Todo.CONTENT_URI, "_id=?", param);
		return result;
		
	}

	
	
	public static final int updateParentComplete(ContentResolver resolver, int complete, int parentId){
		ContentValues content = new ContentValues();
		content.put(Todos.Todo.DONE, complete);
		
		Date now = new Date();
		String time = new StringBuffer("").append(now.getTime()).toString();
		
		content.put(Todos.Todo.MODIFIED_DATE, time);
		
		return resolver.update(Todos.Todo.CONTENT_URI, content, "parent_id=?", new String[]{Integer.toString(parentId)});
	}
	
	public static ArrayList<TaskContent> getChildTasks(ArrayList<TaskContent> items, int selectedItemId) {
		ArrayList<TaskContent> result = new ArrayList<TaskContent>();
		for (TaskContent t : items) {
			if (t.getId() == selectedItemId) {
				result = t.getChild();
				break;
			}
		}
		return result;
	}
	
	public static final ArrayList<TaskContent> getChildTasks(int parentId, ContentResolver resolver, String sortOrder) {
		ArrayList<TaskContent> result = new ArrayList<TaskContent>();

		Cursor cursor = resolver.query(Todos.Todo.CONTENT_URI, null, "parent_id=" + parentId, null, sortOrder);
		if (cursor != null) {
			Log.d("TodoAdapter", "CURSOR IS NOT NULL");
			cursor.moveToFirst();
			Log.d("TodoAdapter", "CURSOR RESULT: " + cursor.getCount());
			while (cursor.isAfterLast() == false) {
				TaskContent o = convertToTaskContent(cursor);
				result.add(o);
				cursor.moveToNext();
			}
			cursor.close();
		}

		return result;
	}
	
	/* get parent then get child for each
	 * 
	 */
	public static final ArrayList<TaskContent> getParentTaskContent(ContentResolver resolver, String sortOrder) {
		ArrayList<TaskContent> result = new ArrayList<TaskContent>();

		Cursor cursor = resolver.query(Todos.Todo.CONTENT_URI, null, "parent_id is null", null, sortOrder);
		if (cursor != null) {
			cursor.moveToFirst();
			while (cursor.isAfterLast() == false) {
				TaskContent o = convertToTaskContent(cursor);
				//get child
				ArrayList<TaskContent> child = TodoAdapter.getChildTasks(o.getId(), resolver, sortOrder);
				if (child.size() > 0) {
					o.setChild(child);
				}
				result.add(o);
				cursor.moveToNext();
			}
			cursor.close();
		}
		
		return result;
	}
	
	public static final TaskContent getTaskContent(ContentResolver resolver, int id) {
		TaskContent result = new TaskContent();

		Cursor cursor = resolver.query(Todos.Todo.CONTENT_URI, null, "_id=" + id, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
			while (cursor.isAfterLast() == false) {
				result = convertToTaskContent(cursor);
				cursor.moveToNext();
			}
			cursor.close();
		}
		
		return result;
	}
	
	private static final TaskContent convertToTaskContent(Cursor cursor){
		/*
		private String title;
		private String note;
		private int priority;
		private Date dueDate;
		private Date createdDate;	
		private Date modifiedDate;
		private int id;
		private int parentId;
		private int done;
		*/
		TaskContent o = new TaskContent();
		o.setId(cursor.getInt(cursor.getColumnIndex(Todos.Todo._ID)));
		o.setTitle(cursor.getString(cursor.getColumnIndex(Todos.Todo.TITLE)));
		o.setPriority(cursor.getInt(cursor.getColumnIndex(Todos.Todo.PRIORITY)));
		
		o.setDueDate(Util.convertStringToDate(cursor.getString(cursor.getColumnIndex(Todos.Todo.DUE_DATE))));
		o.setCreatedDate(Util.convertStringToDate(cursor.getString(cursor.getColumnIndex(Todos.Todo.CREATED_DATE))));
		o.setModifiedDate(Util.convertStringToDate(cursor.getString(cursor.getColumnIndex(Todos.Todo.MODIFIED_DATE))));
		
		Log.d("RETRIEVE PARENT ID:", cursor.getInt(cursor.getColumnIndex(Todos.Todo.PARENT_ID)) + " NAH LO");
		o.setParentId(cursor.getInt(cursor.getColumnIndex(Todos.Todo.PARENT_ID)));
		o.setDone(cursor.getInt(cursor.getColumnIndex(Todos.Todo.DONE)));
		
		return o;
	}
}
